import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import svgLoader from "vite-svg-loader";
import vueJsx from "@vitejs/plugin-vue-jsx";
import removeConsole from "vite-plugin-remove-console";
import { resolve } from "path";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";

// https://vitejs.dev/config/

/** 当前执行node命令时文件夹的地址（工作目录） */
const root: string = process.cwd();

/** 路径查找 */
const pathResolve = (dir: string): string => {
	return resolve(__dirname, ".", dir);
};

/** 设置别名 */
const alias: Record<string, string> = {
	"@": pathResolve("src"),
};

const pathSrc = resolve(__dirname, "src");

export default defineConfig(async () => ({
	plugins: [
		vue(),
		// jsx、tsx语法支持
		vueJsx(),
		// 线上环境删除console
		removeConsole({ external: ["src/assets/iconfont/iconfont.js"] }),
		// svg组件化支持
		svgLoader(),
		// 自动导入
		AutoImport({
			// 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
			imports: ["vue"],
			// 自动导入 AntDesign 相关函数
			resolvers: [
				// 自动导入 AntDesign 组件
				AntDesignVueResolver({
					importStyle: false, // css in js
				}),
				// 自动导入图标组件
				IconsResolver({
					prefix: "Icon",
				}),
			],
			dts: resolve(pathSrc, "auto-imports.d.ts"),
		}),
		Components({
			resolvers: [
				// 自动注册图标组件
				IconsResolver({
					enabledCollections: ["ep"],
				}),
				// 自动导入 AntDesign 组件
				AntDesignVueResolver({
					importStyle: false, // css in js
				}),
			],
			dts: resolve(pathSrc, "components.d.ts"),
		}),
		Icons({
			autoInstall: true,
		}),
	],
	root,
	resolve: {
		alias,
	},
	// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
	//
	// 1. prevent vite from obscuring rust errors
	clearScreen: false,
	// 2. tauri expects a fixed port, fail if that port is not available
	server: {
		port: 1420,
		strictPort: true,
		watch: {
			// 3. tell vite to ignore watching `src-tauri`
			ignored: ["**/src-tauri/**"],
		},
	},
}));
